/**
 * 3η ΓΕ 2013-2014 - Super Market
 * ΦΡΑΓΚΟΥΛΟΠΟΥΛΟΥ ΕΜΜΑΝΟΥΕΛΑ (69298)
 * ΧΑΛΔΑΣ ΧΑΡΑΛΑΜΠΟΣ (69305)
 * ΧΟΡΜΟΒΙΤΗΣ ΙΩΑΝΝΗΣ (69325)
 * ΧΟΥΣΤΟΥΛΑΚΗ ΕΥΑΓΓΕΛΙΑ (69327)
 */ 
package administratorGUI;

import java.util.List;
import javax.persistence.TypedQuery;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
import model.Store;
import supermarket.EntityManagerFactory;
import supermarket.JTextFieldLimit;

/**
 * Οθόνη Εισαγωγής / Επεξεργασίας Καταστήματος
 */
public class NewStoreDialog extends javax.swing.JDialog {

    private boolean mSave = false;
    private boolean isNewRecord;
    private Store store;

    /**
     * Constructor
     */
    public NewStoreDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initForm();
    }

    /**
     * Ορίζει το Max Length στα JTextFields
     */
    private void initForm() {
        storeName.setDocument(new JTextFieldLimit(50));
        storeAddress.setDocument(new JTextFieldLimit(100));
        this.isNewRecord = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        storeName = new javax.swing.JTextField();
        storeNameLabel = new javax.swing.JLabel();
        storeAddressLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        storeAddress = new javax.swing.JTextArea();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Κατάστημα");

        storeName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        storeNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        storeNameLabel.setText("Όνομα καταστήματος");

        storeAddressLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        storeAddressLabel.setText("Διεύθυνση");

        storeAddress.setColumns(20);
        storeAddress.setLineWrap(true);
        storeAddress.setRows(5);
        storeAddress.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jScrollPane1.setViewportView(storeAddress);

        cancelButton.setText("Ακύρωση");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Εγγραφή");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1)
                    .add(storeName)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(storeNameLabel)
                            .add(storeAddressLabel))
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(0, 197, Short.MAX_VALUE)
                        .add(saveButton)
                        .add(18, 18, 18)
                        .add(cancelButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(storeNameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(storeName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(storeAddressLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(saveButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Button: Εγγραφή
     *
     * @param evt
     */
     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
         // Έλεγχος για υποχρεωτικά πεδία
         if (storeName.getText().trim().isEmpty()) {
             JOptionPane.showMessageDialog(this, "Το πεδίο '"
                     + storeNameLabel.getText() + "' είναι υποχρεωτικό.",
                     "Προσοχή", WARNING_MESSAGE);
         } else {
             // Ελέγχω αν το 'Όνομα καταστήματος' είναι unique
             TypedQuery<Store> query = EntityManagerFactory.getEntityManager()
                     .createNamedQuery("Store.findByName", Store.class)
                     .setParameter("name", this.getStoreName());
             List<Store> list = query.getResultList();

             // Αν δεν υπάρχει εγγραφή στη βάση  
             if (list.isEmpty()) {
                 this.dispose();
                 this.mSave = true;
             }
             else {
                 // Ειδικά για edit mode, ελέγχω ότι βρέθηκε εγγραφή
                 // με διαφορετικό id
                 if (this.isNewRecord || !this.isNewRecord
                         && list.get(0).getStoreId() != this.store.getStoreId()) {
                     
                         JOptionPane.showMessageDialog(this, "Έχετε ήδη καταχωρήσει "
                                 + "το κατάστημα με όνομα: '" + this.getStoreName() + "'",
                                 "Προσοχή", WARNING_MESSAGE);
                } else {
                    dispose();
                    mSave = true;
                }
             }
         }
     }//GEN-LAST:event_saveButtonActionPerformed

    /**
     * Button: Ακύρωση
     *
     * @param evt
     */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
        mSave = false;
    }//GEN-LAST:event_cancelButtonActionPerformed

    boolean isValidForSave() {
        return mSave;
    }

    /**
     * Επιστρέφει την τιμή στο πεδίο 'Όνομα καταστήματος'
     *
     * @return
     */
    String getStoreName() {
        return storeName.getText().trim();
    }

    /**
     * Επιστρέφει την τιμή στο πεδίο 'Διεύθυνση'
     *
     * @return
     */
    String getStoreAddress() {
        return storeAddress.getText().trim();
    }

    /**
     * Γεμίζει τα πεδία της οθόνης (σε edit mode)
     *
     * @param store
     */
    public void setStore(Store store) {
        this.store = store;
        this.isNewRecord = false;
        storeName.setText(store.getName().trim());
        storeAddress.setText(store.getAddress().trim());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextArea storeAddress;
    private javax.swing.JLabel storeAddressLabel;
    private javax.swing.JTextField storeName;
    private javax.swing.JLabel storeNameLabel;
    // End of variables declaration//GEN-END:variables
}
